package com.x.erp.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.x.erp.model.CarLocation;
import com.x.framework.dao.BaseDao;

/**
 * 车牌所在地信息DAO
 */
@Repository
public class CarLocationDao extends BaseDao {

	private final static String selectOrderListSql = "SELECT T1.* FROM BASE_CAR_LOCATION T1 WHERE true ";
	/**
	 * 查询车牌所在地列表
	 * @param carLocation
	 * @return
	 * @throws Exception
	 */
	public List<CarLocation> selectCarLocationList(CarLocation carLocation) throws Exception{
		StringBuilder sql = new StringBuilder(selectOrderListSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, carLocation);
		return this.queryForList(sql.toString(), CarLocation.class, params);
	}

	private final static String selectOrderNumSql = "SELECT COUNT(T1.LOCATION_ID) FROM BASE_CAR_LOCATION T1 WHERE true ";
	/**
	 * 查询车牌所在地数量
	 * @param carLocation
	 * @return
	 * @throws Exception
	 */
	public int selectCarLocationNum(CarLocation carLocation) throws Exception{
		StringBuilder sql = new StringBuilder(selectOrderNumSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, carLocation);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param carLocation
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, CarLocation carLocation) {
		if(carLocation != null) {
			if(carLocation.getLocationId() != null){
				sql.append("AND T1.LOCATION_ID = ? ");
				params.add(carLocation.getLocationId());
			}
			if(StringUtils.isNotEmpty(carLocation.getLocationCode())){
				sql.append("AND T1.LOCATION_CODE = ? ");
				params.add(carLocation.getLocationCode());
			}
		}
	}
}
